import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
import "./UI"
import QtMultimedia 5.5
import QtQuick.Dialogs 1.2
import QtQuick.Window 2.0

ApplicationWindow {
    visible: true
    width: Screen.desktopAvailableWidth*4/5
    height: Screen.desktopAvailableHeight*4/5
    title: qsTr("Music rhythm editor")
    id:mainwin

    property bool exitedProject:false
    property bool playinyState:false
    property url soundUrl;
    property string fileName;
    property string filePath;
    property string songName;
    property var music_duration:180.000
    property int spotNum;

    ListModel{
        id:datasModel

    }

    function saveData(){

    }

    menuBar: MenuBar {
        Menu {
            title: qsTr("文件")
            MenuItem {
                text: qsTr("&打开MP3文件")
                onTriggered: {
                    mdialog.open();
                }

            }
            MenuItem {
                text: qsTr("&保存")
                onTriggered: {
                    spotNum=datasModel.count;
                    DataManager.setRhythmData(songName,music_duration*1000,spotNum);

                    var pad_width=music_duration*50;

                    for(var a=0;a<datasModel.count;a++){
                        var time=datasModel.get(a).pos*music_duration*1000/pad_width;
                        var end_time=(datasModel.get(a).pos+datasModel.get(a).length)*music_duration*1000/pad_width;
                        var pos=datasModel.get(a).pindex;
                        var mode=datasModel.get(a).mode
                        DataManager.addSpotData(time,pos,mode,end_time);
                    }

                    DataManager.comressDataToJson();

                    DataManager.saveDatas(fileName,filePath);
                }
            }

        }
    }

    Rectangle{
        anchors.fill: parent
        id:mainrec
        MusicPad{
            anchors.fill: parent
            id:musicPad
            focus: true
        }

        Rectangle{
            anchors.centerIn: parent
            width: parent.width
            height:1
            color:"#777777"

        }
        Row{
            anchors.centerIn: parent
            spacing: 20
            Rectangle{
                color:"#fafafa"
                border.width: 1
                border.color: "#777777"
                radius: width/2
                width: 50
                height: width
                anchors.verticalCenter: parent.verticalCenter
                Image{
                    anchors.fill: parent
                    fillMode: Image.PreserveAspectFit
                    source:"qrc:/imgs/stop.png"

                }
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        AudioData.stopMusic();
                        playinyState=false;
                        musicPad.setPosition(0);
                    }
                }
            }
            Rectangle{
                color:"#fafafa"
                border.width: 1
                border.color: "#777777"
                radius: width/2
                width: 70
                height: width
                Image{
                    anchors.fill: parent
                    fillMode: Image.PreserveAspectFit
                    source:playinyState?"qrc:/imgs/pause.png":"qrc:/imgs/play.png"
                }
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        if(playinyState){
//                            player.pause();
                            AudioData.pauseMusic();
                        }
                        else{
//                            player.play();
                            AudioData.playMusic();
                        }

                        playinyState=AudioData.isPlaying;
                    }
                }
            }
            Rectangle{
                color:"#fafafa"
                border.width: 1
                border.color: "#777777"
                radius: width/2
                width: 50
                height: width
                anchors.verticalCenter: parent.verticalCenter
                Image{
                    anchors.fill: parent
                    fillMode: Image.PreserveAspectFit
                    source:"qrc:/imgs/restart.png"
                }
            }
        }

    }


    Audio {
        id: player
    }

    FileDialog{
        id:mdialog
        folder: shortcuts.home
        nameFilters: [ "Sound files (*.mp3)" ]
        onAccepted:{
            soundUrl=mdialog.fileUrl;
            player.source=soundUrl;
            AudioData.setSource(soundUrl.toString().substring(8,soundUrl.toString().length));
            initTimer.start();

            var strs=soundUrl.toString();
            var strslist=strs.split("/");
            fileName=strslist[strslist.length-1];
            songName=fileName.substring(0,fileName.length-4);
            fileName=fileName.substring(0,fileName.length-4)+".txt";
            filePath=folder.toString();
            filePath=filePath.substring(8,filePath.length);
        }
    }


    //由于没有获取player的加载事件，所以就延时设置了
    Timer{
        id:initTimer
        interval: 1500
        repeat: false
        onTriggered: {
            musicPad.setDuration(player.duration);
        }
    }

    //更新当前的播放位置
    Timer{
        id:refreshTimer
        interval: 10
        repeat: playinyState
        running: playinyState
        onTriggered: {
            if(playinyState){
                musicPad.setPosition(AudioData.position);
                musicPad.drawAmplitude(AudioData.amplitude);
//                console.log(AudioData.amplitude);
            }
        }
    }


}
